package biz;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

import dao.TCustCharge;

public class CustChargeBiz {
	Configuration configuration = null;
	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction transaction = null;
	
	public void AddCustCharge(TCustCharge sarv){
		ModCustCharge(sarv);
	}
	
	public void ModCustCharge(TCustCharge sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelCustCharge(TCustCharge sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TCustCharge tddmp = (TCustCharge)session.get(TCustCharge.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetCustCharge(String clientid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TCustCharge as f where f.clientid='" + clientid +"' order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetCustChargeById(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TCustCharge as f where f.id ='" + oid + "' order by f.id desc");
		
		List list =query.list();
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
